package com.lkm.market2_0;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.TypedValue;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.module.event.ModuleInitEvent;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.task2.TaskManager;
import com.lkm.frame.util.CycleHelp;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.appserver.AppMainModule;
import com.lkm.market2_0.m.imple.steelyarddata.DataManager;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.netapi.Api;
import com.lkm.market2_0.steelard_server.ReceiveTask;
import com.lkm.market2_0.steelard_server.sendtask.SetSteelyardOkTask;
import com.lkm.market2_0.steelyard.OnSteelyardBluetoothStateEvent;
import com.lkm.market2_0.steelyard.SteelyardServerModuleBluetooth;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lkm on 18/1/5.
 */

public class MainActivity extends BaseFragmentActivity {


    public static String localWifi = "market2.0";
    public static String localWifiPass = "ast12345678";

    public static MainActivity mainActivity;
    public static TextView tv_logs;
    public static EditText tv_url,tv_blue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainActivity = this;
        tv_logs = (TextView) findViewById(R.id.tv_logs);
        tv_url = (EditText) findViewById(R.id.tv_url);
        tv_blue = (EditText) findViewById(R.id.tv_blue);

//        20:17:01:10:45:12 原始
//        as:t7:01:10:45:10 邻里汇？
//        as:t2:01:80:51:45:19 18-5-14 广州源生新街市  黎工要的测试市场 目前不知道是哪个市场
//        as:t0:20:18:05:18:15:03 //FSXLHXCS 佛山兴塱海鲜超市
//        as:t0:20:18:05:24:09:10 天河汇
        Api.initKey("as:t7:01:10:45:10");//("20:17:01:10:45:12");
        main();


        ((CheckBox) findViewById(R.id.cb_printOnly)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    UIP.printMac = defaultSteelyardMac;

                    if (StringHelp.isEmpty(defaultSteelyardMac) || "全部".equals(defaultSteelyardMac)) {
                        UIP.printMac = null;
                    }
                } else {
                    UIP.printMac = null;
                }
            }
        });

        getWifiInfo();
        re(this);

        tv_url.setText(Api.host);


        String blue = getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("tv_blue", "");
        tv_blue.setText(blue);

        if (!StringHelp.isEmpty(blue)) {
            SteelyardServerModuleBluetooth moduleBluetooth = (SteelyardServerModuleBluetooth) MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
            if(moduleBluetooth==null){
                MyApplication.getInstance().getModuleManager().getModule(SteelyardServerModule.class, new RunnablePR<Boolean, Void>() {
                    @Override
                    public Void run(Boolean aBoolean) {

                        if(!aBoolean)return null;
                        SteelyardServerModuleBluetooth moduleBluetooth = (SteelyardServerModuleBluetooth) MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
                        moduleBluetooth.startServer(blue);
                        return null;
                    }
                });
            }else
            moduleBluetooth.startServer(blue);
        }


        LinkBusCycleWrapper.register(new Observer<OnSteelyardBluetoothStateEvent>() {

            @Override
            public Class<OnSteelyardBluetoothStateEvent> getBusEventClass() {
                return OnSteelyardBluetoothStateEvent.class;
            }

            @Override
            public void onReceiver(OnSteelyardBluetoothStateEvent data, Runnable nextRun) {
                try {
                    String s = "";
                    if (data.isConnecFail()) {
                        s = "蓝牙：连接失败";
                    } else if (data.isConnecting()) {
                        s = "蓝牙：连接中";
                    } else if (data.isConnecSuccess()) {
                        s = "蓝牙：连接成功";
                    } else if (data.isDisConnect()) {
                        s = "蓝牙：连接断开";
                    }

                    ((TextView) findViewById(R.id.tv_bluetips)).setText(s);
                } finally {
                    nextRun.run();
                }

            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);

    }
    public void sendOrder(View v){
        Intent intent = new Intent("com.ast.market2_0.action.ORDER");
        intent.putExtra("order", "{\"od\":\"15000000010000000215\",\"ty\":1,\"tp\":4.29,\"gs\":[{\"up\":10.0,\"w\":0.429,\"ad\":8651596,\"zad\":8319676,\"ozad\":8592585,\"n\":\"红枣\",\"id\":\"88\"}],\"ct\":\"2019-01-25 15:26:03\",\"ukey\":\"495513360667\"}");
        MyApplication.getInstance().sendBroadcast(intent);//,"com.ast.market2_0.permission.RECV_ORDER");// OrderedBroadcast("intent");
    }

    public void onClickSetUrl(View v) {
        String ip = tv_url.getText().toString();
        MyApplication.getInstance().getSharedPreferences("settingapp",Context.MODE_PRIVATE).edit().putString("tv_url", ip).commit();
        Api.host = ip;
    }

    public void onClickBlue(View v) {
        String ip = tv_blue.getText().toString();
        MyApplication.getInstance().getSharedPreferences("settingapp",Context.MODE_PRIVATE).edit().putString("tv_blue", ip).commit();
        SteelyardServerModuleBluetooth moduleBluetooth = (SteelyardServerModuleBluetooth) MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        moduleBluetooth.startServer(ip);

    }
    public void onClickDisBlue(View v) {

        SteelyardServerModuleBluetooth moduleBluetooth = (SteelyardServerModuleBluetooth) MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        moduleBluetooth.stopServer();

    }

    @Override
    protected void onDestroy() {
        if (mainActivity == this) {
            mainActivity = null;
            tv_logs = null;
        }
        super.onDestroy();
    }

    public static void main() {
        byte ff = (byte) 0xff;
        ff += 1;
        System.out.println("=====" + ff);
        ff += 1;
        System.out.println("=====" + ff);
    }

    private String defaultSteelyardMac;

    public void onClickSelect(final View v) {

        SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
        if (steelyardStatusManagerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

        StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();


        final String[] lists = new String[statusInfos.length + 1];
        lists[0] = "全部";

        for (int i = 0; i < statusInfos.length; i++) {
            StatusInfo si = statusInfos[i];
            lists[i + 1] = si.getSteelyardMac();

        }


        ViewUIHelp.showSingleList(this, lists, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which >= 0)
                    defaultSteelyardMac = lists[which];
                ((TextView) v).setText("操作的称：" + defaultSteelyardMac);
                if (((CheckBox) findViewById(R.id.cb_printOnly)).isChecked()) {
                    UIP.printMac = defaultSteelyardMac;
                }
            }
        });

    }

    public void onClickFont1(View v) {
        tv_logs.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_logs.getTextSize() + 2);
    }

    public void onClickFont2(View v) {

        tv_logs.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_logs.getTextSize() - 2);
    }

    public void onClickClearConsole(View v) {
        UIP.clear();
    }

    public void onClickGetStatus(View v) {

        SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);

        if (steelyardStatusManagerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

        StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();

        int countZF = 0;
        int countOutline = 0;
        int countYC = 0;

        for (int i = 0; i < statusInfos.length; i++) {
            StatusInfo si = statusInfos[i];
            int status = 1;
            /// 上报状态1正常，2停用，3异常，4关闭，5拆机，99强制性锁定
            if (si.isOnline()) {
                status = 1;
            } else {
                status = 4;
            }
            if (si.isException()) {
                status = 3;
            }

            if (status == 1) {
                countZF++;
            } else if (status == 4) {
                countOutline++;
            } else {
                countYC++;
            }
        }


        ((TextView) findViewById(R.id.tv_tips)).setText(
                "全部的：" + statusInfos.length +
                        "\t     正常的：" + countZF +
                        "\t     掉线的：" + countOutline +
                        "\t     异常的：" + countYC);


//        byte[] mings=DataEncryptionHelp.Decrypt(new byte[]{0x33,0x3e,0x34,0x3e,0x35,0x3e,0x37,0x36,0x37,0x3f,0x38,0x3d,0x3a,0x39,0x31,0x30,0x33,0x3a, 0x33,0x3e ,0x3a,0x39, 0x33,0x3e},(byte) 0x00,24);
//
//        P.p(NumberHelp.bytesTohexDesc(mings));
//        P.p(
//                NumberHelp.bytesTohexDesc(
//        DataEncryptionHelp.Encrypt(mings,(byte)0x00,12))
//        );


    }


    public void onClickUpStatus(View v) {

        SteelyardStatusManagerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }
        mSteelyardServerModule.upStatusToServer();

//        UpStatusTask.up(mSteelyardServerModule);
    }

    public void onClickUpData(View v) {
        DataManager.getInstance().updataData();
    }


    public void onClickSendRepairing(View v) {
        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }


        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                mSteelyardServerModule.setSteelyardRepairing(si.getSteelyardMac());
            }
        } else {
            mSteelyardServerModule.setSteelyardRepairing(SteelyardMac);
        }

    }

    public void onClickSendOk(View v) {
        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }


        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                mSteelyardServerModule.setSteelyardOK(si.getSteelyardMac());
            }
        } else {
            mSteelyardServerModule.setSteelyardOK(SteelyardMac);
        }


        TaskManager taskManager=com.lkm.market2_0.Config.instance.getTaskManager2();
//        "readTask"+clientSessionId
        int[] ses=taskManager.getRunTask(ReceiveTask.createId("/dev/ttyS4"));

        ReceiveTask receiveTask=null;
        if(ses!=null){
            receiveTask = (ReceiveTask)taskManager.getRunTask(ses[0]);
        }
//        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);
        if(receiveTask!=null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();
            SetSteelyardOkTask setSteelyardOkTask=new SetSteelyardOkTask(new DefaultTaskExecutor<Object[], Void, Boolean>(true),com.lkm.market2_0.Config.instance.getTaskManager2(),iHardwareControler,receiveTask);
            setSteelyardOkTask.action();
            setSteelyardOkTask.execTask();
//            return setSteelyardOkTask;
        }


    }

    public void onClickSetDate(View v) {
        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                mSteelyardServerModule.setDate(si.getSteelyardMac());
            }
        } else {
            mSteelyardServerModule.setDate(SteelyardMac);
        }

    }

    public void onClickSendPrint(View v) {
        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                SteelyardMac = si.getSteelyardMac();
                MyApplication.getInstance().getMainPresenter().sendPrint(new String[]{SteelyardMac});

//                mSteelyardServerModule.print(SteelyardMac,"qdf2哈哈","子标题223");
            }
        } else {
            MyApplication.getInstance().getMainPresenter().sendPrint(new String[]{SteelyardMac});
//            mSteelyardServerModule.print(SteelyardMac,"qdf2哈哈","子标题223");
        }

    }

    public void onClickSendTitle(View v) {
        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }
        SteelyardDataModule mSteelyardDataModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardDataModule.class);
        if (mSteelyardDataModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                SteelyardMac = si.getSteelyardMac();
                MyApplication.getInstance().getMainPresenter().sendTitle(new String[]{SteelyardMac});

//                SteelyardInfo steelyardInfo=mSteelyardDataModule.getSteelyardInfo(SteelyardMac);
//                String title=AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg()).getName();
//                String title2=steelyardInfo.getStallEntity().getStallName();
//                mSteelyardServerModule.setTitle(SteelyardMac,title,title2);
            }

        } else {
            MyApplication.getInstance().getMainPresenter().sendTitle(new String[]{SteelyardMac});
//            SteelyardInfo steelyardInfo=mSteelyardDataModule.getSteelyardInfo(SteelyardMac);
//            String title=AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg()).getName();
//            String title2=steelyardInfo.getStallEntity().getStallName();
//            mSteelyardServerModule.setTitle(SteelyardMac,title,title2);
        }


    }

    public void onClickSend(View v) {
        String SteelyardMac = defaultSteelyardMac;

        AppMainModule mAppMainModule = MyApplication.getInstance().getModuleManager().loadModule(AppMainModule.class);

        if (mAppMainModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }


        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                SteelyardMac = si.getSteelyardMac();
                mAppMainModule.syncGoodsButtonToSteelyardForS(SteelyardMac);
            }
        } else {
            mAppMainModule.syncGoodsButtonToSteelyardForS(SteelyardMac);
        }


    }

    Dialog dialog_wifiset;

    public void onClickSetWifiConfig(View v) {
        dialog_wifiset = ViewUIHelp.showAlertDialog(this, R.layout.dialog_wifiset, "", "设置", "取消", new Runnable() {
            @Override
            public void run() {
                String wifiName = ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiname)).getText().toString();
                String wifiPass = ((EditText) dialog_wifiset.findViewById(R.id.tv_wifipass)).getText().toString();
                String wifiModuleBaudRate = ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiModuleBaudRate)).getText().toString();
                String ip = ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiip)).getText().toString();
                String port = ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiport)).getText().toString();
                String wifiModel = ((RadioGroup) dialog_wifiset.findViewById(R.id.rg_wifi)).getCheckedRadioButtonId()==R.id.rb_new?"new":"old";

                Map<String, String> m = new HashMap<>();
                m.put("wifiName", wifiName);
                m.put("wifiPass", wifiPass);
                m.put("wifiModuleBaudRate", wifiModuleBaudRate);
                m.put("ip", ip);
                m.put("port", port);
                m.put("wifiModel", wifiModel);
                MainActivity.this.getPreferences(Context.MODE_PRIVATE).edit().putString("p_wifiset", new Gson().toJson(m)).apply();

                dialog_wifiset = null;
                setWifiConfig(wifiName, wifiPass, wifiModuleBaudRate, ip, port, wifiModel);
            }
        }, new Runnable() {
            @Override
            public void run() {
                dialog_wifiset = null;
            }
        });

        Map<String, String> m = new Gson().fromJson(MainActivity.this.getPreferences(Context.MODE_PRIVATE).getString("p_wifiset", "{}"), new com.google.gson.reflect.TypeToken<HashMap<String, String>>() {
        }.getType());
//        zhongxing3
//                zx89889842
//        192.168.0.152
//        47111
        ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiname)).setText(StringHelp.isEmpty(m.get("wifiName")) ? "zhongxing3" : m.get("wifiName"));
        ((EditText) dialog_wifiset.findViewById(R.id.tv_wifipass)).setText(StringHelp.isEmpty(m.get("wifiPass")) ? "zx89889842" : m.get("wifiPass"));
        ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiModuleBaudRate)).setText(StringHelp.isEmpty(m.get("wifiModuleBaudRate")) ? "115200" : m.get("wifiModuleBaudRate"));
        ((EditText)dialog_wifiset.findViewById(R.id.tv_wifiip)).setText(StringHelp.isEmpty(m.get("ip"))?"192.168.0.152":m.get("ip"));
        ((EditText)dialog_wifiset.findViewById(R.id.tv_wifiport)).setText(StringHelp.isEmpty(m.get("port"))?"47111":m.get("port"));


        String wifiModel = m.get("wifiModel");
//        ((RadioGroup) dialog_wifiset.findViewById(R.id.rg_wifi)).getCheckedRadioButtonId()==R.id.rb_new?"new":"old";

        ((RadioGroup) dialog_wifiset.findViewById(R.id.rg_wifi)).check("old".equals(wifiModel)?R.id.rb_old:R.id.rb_new);

//        ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiip)).setText("121.201.16.170");
//        ((EditText) dialog_wifiset.findViewById(R.id.tv_wifiport)).setText("47111");
    }

    public void setWifiConfig(String wifiName, String wifiPass, String wifiModuleBaudRate, String ip, String port, String wifiModel) {
        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);
        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

//        +++；AT+WMODE=STA；AT+WSTA=market2.0,ast12345678；AT+UART=115200,1,NONE；AT+SOCK=TCPC,192.168.43.1,47111；


        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                SteelyardMac = si.getSteelyardMac();
                mSteelyardServerModule.setWifiConfig(SteelyardMac, wifiName, wifiPass, wifiModuleBaudRate, ip, port, wifiModel);
            }
        } else {
            mSteelyardServerModule.setWifiConfig(SteelyardMac, wifiName, wifiPass, wifiModuleBaudRate, ip, port, wifiModel);
        }


    }

    public void onClickSenddd(View v) {

        String SteelyardMac = defaultSteelyardMac;

        SteelyardServerModule mSteelyardServerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardServerModule.class);

        if (mSteelyardServerModule == null) {
            ViewUIHelp.showTips(this, "系统启动中，请稍后操作");
            return;
        }

        List<GoodsButtonInfoTo> goodsButtonInfoTos = new ArrayList<>();


        for (int i = 0; i < 700; i++) {
            goodsButtonInfoTos.add(new GoodsButtonInfoTo("xxx" + i, 22.2f));
        }

//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));
//        goodsButtonInfoTos.add(new GoodsButtonInfoTo("e3wf搜索",222));


        if (StringHelp.isEmpty(SteelyardMac) || "全部".equals(SteelyardMac)) {
            SteelyardStatusManagerModule steelyardStatusManagerModule = MyApplication.getInstance().getModuleManager().loadModule(SteelyardStatusManagerModule.class);
            StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();
            for (int i = 0; i < statusInfos.length; i++) {
                StatusInfo si = statusInfos[i];
                SteelyardMac = si.getSteelyardMac();
                mSteelyardServerModule.setGoodsButton(SteelyardMac, goodsButtonInfoTos);
            }
        } else {
            mSteelyardServerModule.setGoodsButton(SteelyardMac, goodsButtonInfoTos);
        }

    }

    private void getWifiInfo() {
        WifiManager my_wifiManager;
        WifiInfo wifiInfo;

        my_wifiManager = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE));
        wifiInfo = my_wifiManager.getConnectionInfo();

        ((TextView) findViewById(R.id.tv_localIp)).setText(intToIp(wifiInfo.getIpAddress()));
    }

    public static String intToIp(int paramInt) {
        return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
                + (0xFF & paramInt >> 24);
    }

    public void re(Context context) {
        //WIFI状态接收器
        WifiStateReceiver wifiReceiver = new WifiStateReceiver(context);
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        context.registerReceiver(wifiReceiver, filter);
    }

    class WifiStateReceiver extends BroadcastReceiver {

        Context context;

        public WifiStateReceiver(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context;

        }

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            System.out.println(intent.getAction());

            WifiManager my_wifiManager;
            WifiInfo wifiInfo;

            my_wifiManager = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE));
            wifiInfo = my_wifiManager.getConnectionInfo();

            boolean isDISABLED = false;

            if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                System.out.println("网络状态改变");
                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {//如果断开连接
                    isDISABLED = true;
                }
            } else if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                //WIFI开关
                int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
                if (wifistate == WifiManager.WIFI_STATE_DISABLED) {//如果关闭
                    isDISABLED = true;
                }
            }
            getWifiInfo();
        }
    }

    public void onClickOpenWifiAp(View v) {
        setWifiApEnabled(this, localWifi, localWifiPass, true);
    }

    /* 开启/关闭热点 */
    public static boolean setWifiApEnabled(final Context context, String ssid, String password, final boolean enabled) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (enabled) {
            // 因为wifi和热点不能同时打开，所以打开热点的时候需要关闭wifi
            wifiManager.setWifiEnabled(false);
        }

        if (Build.VERSION.SDK_INT >= 26) {
            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setWifiApEnabledForAndroidO(context, enabled);
                }
            });
            return true;
        }

        WifiConfiguration ap = null;

        try {
            // 热点的配置类
            WifiConfiguration apConfig = new WifiConfiguration();
            // 配置热点的名称(可以在名字后面加点随机数什么的)
            apConfig.SSID = ssid;
            apConfig.preSharedKey = password;
            apConfig.allowedKeyManagement.set(4);//设置加密类型，这里4是wpa加密

            Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            // 返回热点打开状态
            return (Boolean) method.invoke(wifiManager, apConfig, enabled);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 8.0 开启热点方法
     * 注意：这个方法开启的热点名称和密码是手机系统里面默认的那个
     *
     * @param context
     */
    public static void setWifiApEnabledForAndroidO(Context context, boolean isEnable) {
        ConnectivityManager connManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        Field iConnMgrField = null;
        try {
            iConnMgrField = connManager.getClass().getDeclaredField("mService");
            iConnMgrField.setAccessible(true);
            Object iConnMgr = iConnMgrField.get(connManager);
            Class<?> iConnMgrClass = Class.forName(iConnMgr.getClass().getName());

            if (isEnable) {
                Method startTethering = iConnMgrClass.getMethod("startTethering", int.class, ResultReceiver.class, boolean.class);
                startTethering.invoke(iConnMgr, 0, null, true);
            } else {
                Method startTethering = iConnMgrClass.getMethod("stopTethering", int.class);
                startTethering.invoke(iConnMgr, 0);
            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
